﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.Supervise.Model;
using HZ.Supervise.IBLL;
using HZ.Supervise.Model.Query;
using System.Text;

namespace HZ.Supervise.Web.Common
{
	public partial class SelectArea : BasePage
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				BindArea();
			}
		}

		private void BindArea()
		{
			HZ.Supervise.Model.User user = CurrentAccount.GetUser();
			string AreaID = user.Org.Area.AreaID;
			string AreaName = user.Org.Area.AreaName;

			IBLL_Area ia = new HZ.Supervise.BLL.BLL_Area();
			IList<Area> lstArea = new List<Area>();
			int i;

			if (!(AreaID.Substring(4, 2).Equals("00", StringComparison.Ordinal)))
			{

				lstArea = ia.GetAreas(true, AreaID, 1, 0, out i);

			}
			else
			{

				lstArea = ia.GetAreas(false,AreaID, 1, 0, out i);

			}

			//省级
			if (AreaID.Equals(Constants.CURRENT_DEPLOY_AREAID))
			{
				TreeNode nodeParent = new TreeNode();
				//将省份去掉 2011.4.12 赵洁
				//nodeParent.Text = AreaName;
				//nodeParent.Value = AreaID;
				//tvArea.Nodes.Add(nodeParent);

				foreach (HZ.Supervise.Model.Area area in lstArea)
				{
					//省级  
					TreeNode node = new TreeNode();
					node.Text = area.AreaName;
					node.Value = area.AreaID;
					node.Expanded = false;
					// nodeParent.ChildNodes.Add(node);
					if (!node.Text.Equals("省直属"))
					{
						tvArea.Nodes.Add(node);
						AddChildNodes(node);
					}
				}

			}
			else
			{
				//市级
				TreeNode nodeParent = new TreeNode();
				//nodeParent.Text = AreaName;
				//nodeParent.Value = AreaID;
				//tvArea.Nodes.Add(nodeParent);

				if (lstArea.Count > 0)
				{
					foreach (HZ.Supervise.Model.Area area in lstArea)
					{
						//市级
						TreeNode node = new TreeNode();
						node.Text = area.AreaName;

						if (area.AreaID == "420101")
						{
							node.Value = AreaID;
						}
						else
						{
							node.Value = area.AreaID;
						}
						node.Expanded = false;
						// nodeParent.ChildNodes.Add(node);
						tvArea.Nodes.Add(node);
					}
				}
				else
				{
					//区，县级

				}
			}
		}

		private void AddChildNodes(TreeNode node)
		{
			string _strParentNode = node.Value.ToString();

			IBLL_Area ia = new HZ.Supervise.BLL.BLL_Area();
			IList<Area> lstArea = new List<Area>();
			int i;
			lstArea = ia.GetSonAreas(_strParentNode, 1, 0, out i);

			//添加子节点
			foreach (HZ.Supervise.Model.Area area in lstArea)
			{
				TreeNode nodeChild = new TreeNode();
				nodeChild.Text = area.AreaName;
				nodeChild.Value = area.AreaID;
				nodeChild.Expanded = false;
				node.ChildNodes.Add(nodeChild);
				//AddSecondChildNodes(nodeChild);
			}

		}


		/// <summary>
		/// 取得选择地区
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void btnChoose_Click(object sender, EventArgs e)
		{
			StringBuilder sbAreaId = new StringBuilder();
			StringBuilder sbAreaName = new StringBuilder();

			TreeNodeCollection ParentNode = tvArea.CheckedNodes;

			if (ParentNode.Count > 70)
			{
				HZ.Web.WebUtil.Alert("选择数据太多！");
				return;
			}
			else
			{
				foreach (TreeNode node in ParentNode)
				{

					//只保存子节点的选中状态
					if (node.ChildNodes.Count == 0)
					{
						sbAreaId.Append(node.Value);
						sbAreaId.Append(',');
						sbAreaName.Append(node.Text);
						sbAreaName.Append(',');
					}

				}

				//if(sbAreaId.ToString()==""||sbAreaId.ToString()==null)
				//{
				//    Response.Write(" <script>alert('请选择地区') </script> ");
				//    return;
				//}
				ClientScript.RegisterStartupScript(this.GetType(), "标示（任意string，唯一）", " <script>InsertAreaInfo('" + sbAreaName + "','" + sbAreaId + "');</script>");

			}

		}




	}
}
